#include "Sem_V2.hpp"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
#include <ctime>
#include <cstdlib>

int main()
{
    srand((unsigned)time(nullptr));
    std::shared_ptr<SemBuilder> builder = std::make_shared<SemBuilder1>();
    SemEngineer engineer;
    pid_t pid = fork();
    if (pid == 0)
    {
        sleep(5);
        engineer.Construct(builder, SEM_GET);
        std::shared_ptr<Sem> sem = builder->GetSem();
        while (true)
        {
            sem->P();

            printf("C");
            fflush(stdout);
            usleep(rand() % 95270);
            printf("C ");
            usleep(rand() % 43990);
            fflush(stdout);

            sem->V();
        }
        exit(1);
    }
    engineer.Construct(builder,SEM_CREAT, 0666);
    std::shared_ptr<Sem> sem = builder->GetSem();
    while (true)
    {
        sem->P();

        printf("F");
        fflush(stdout);
        usleep(rand() % 95270);
        
        printf("F ");
        usleep(rand() % 43990);
        fflush(stdout);

        sem->V();
    }

    return 0;
}